package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.QueryWithdrawalsAuditDto;
import com.huaxiafinance.management.dto.request.UpdateStateWithDto;
import com.huaxiafinance.management.mapper.PartnerInfoMapper;
import com.huaxiafinance.management.mapper.PartnerWithdrawalsMapper;
import com.huaxiafinance.management.mapper.YfqDictionaryMapper;
import com.huaxiafinance.management.model.PartnerInfo;
import com.huaxiafinance.management.model.PartnerWithdrawals;
import com.huaxiafinance.management.service.WithdrawalsAuditService;
import com.huaxiafinance.management.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 提现申请业务层
 */
@Service("withdrawalsAuditService")
@SuppressWarnings("SpringJavaAutowiringInspection")
public class WithdrawalsAuditServiceImpl implements WithdrawalsAuditService {
    @Autowired
    private PartnerWithdrawalsMapper partnerWithdrawalsMapper;

    @Autowired
    private YfqDictionaryMapper yfqDictionaryMapper;

    @Autowired
    private PartnerInfoMapper partnerInfoMapper;

    /**
     * 提现审核列表
     * @param dto
     * @return
     */
    @Override
    public MessageResult queryWithdrawalsList(QueryWithdrawalsAuditDto dto) {
        MessageResult result=new MessageResult();
        dto.setPage(dto.getPage() == null ? null : dto.getPage() * dto.getRows());
        result.setData(partnerWithdrawalsMapper.queryWithdrawalsList(dto));
        result.setRows(partnerWithdrawalsMapper.countWithdrawalsList(dto));
        result.setSuccessResult();
        return result;
    }


    /**
     * 审核详情
     * @return
     */
    @Override
    public MessageResult getInfo(Integer id) {
        MessageResult result=new MessageResult();
        result.setData(partnerWithdrawalsMapper.getInfo(id));
        result.setSuccessResult();
        return result;
    }

    @Override
    public MessageResult updateState(UpdateStateWithDto dto) {
        MessageResult result=new MessageResult();
        PartnerWithdrawals partnerWithdrawals=partnerWithdrawalsMapper.selectByPrimaryKey(dto.getId());
        PartnerInfo partnerInfo = partnerInfoMapper.selectPartnerInfo(partnerWithdrawals.getUserUuid());
        partnerWithdrawals.setId(dto.getId());
        partnerWithdrawals.setAuditStatus(dto.getState());
        partnerWithdrawals.setRefuseReason(dto.getMsg());
        partnerWithdrawals.setAuditTime(new Date());
        partnerWithdrawals.setAuditorId(dto.getAdminId());
        //取最新银行信息
        partnerWithdrawals.setAccountName(partnerInfo.getAccountName());
        partnerWithdrawals.setBranch(partnerInfo.getBranch());
        partnerWithdrawals.setCardNo(partnerInfo.getCardNo());
        partnerWithdrawals.setBankName(partnerInfo.getBankName());
        //取最新合伙人信息
        partnerWithdrawals.setApplyName(partnerInfo.getName());
        partnerWithdrawals.setApplyCellphone(partnerInfo.getCellphone());
        partnerWithdrawalsMapper.updateByPrimaryKeySelective(partnerWithdrawals);
        //审核拒绝加钱
        if(dto.getState()==4){
            PartnerWithdrawals pw=partnerWithdrawalsMapper.selectByPrimaryKey(dto.getId());
            PartnerInfo info=partnerInfoMapper.queryInfoByUserUuid(pw.getUserUuid());
            info.setBalance(info.getBalance().add(pw.getApplyMoney()));
            info.setFrozenMoney(new BigDecimal("0"));
            partnerInfoMapper.updateByPrimaryKey(info);
        }
        result.setSuccessResult();
        return result;
    }

    @Override
    public MessageResult showRule() {
        //查询提现规则
        MessageResult result=new MessageResult();
        Map a=new HashMap();
        a.put("cycle",yfqDictionaryMapper.selectRelateByCode("1019","1"));
        a.put("second",yfqDictionaryMapper.selectRelateByCode("1019","2"));
        a.put("minimumAmount",yfqDictionaryMapper.selectRelateByCode("1019","3"));
        result.setData(a);
        result.setSuccessResult();
        return result;
    }

    @Override
    public MessageResult updateRule(String cycle, String second, String minimumAmount) {
        MessageResult result=new MessageResult();
        //修改字典表里的提现规则
        yfqDictionaryMapper.updateRelateByCode(cycle,"1019","1");
        yfqDictionaryMapper.updateRelateByCode(second,"1019","2");
        yfqDictionaryMapper.updateRelateByCode(minimumAmount,"1019","3");
        result.setSuccessResult();
        return result;
    }


}
